package com.example.points.controller;

import com.example.points.domain.UserPoints;
import com.example.points.service.UserPointsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/points")
public class UserPointsController {

    @Autowired
    private UserPointsService userPointsService;

    /**
     * 查询用户积分
     */
    @GetMapping("/{userId}")
    public ResponseEntity<UserPoints> getUserPoints(@PathVariable Long userId) {
        UserPoints userPoints = userPointsService.getUserPoints(userId);
        return ResponseEntity.ok(userPoints);
    }

    /**
     * 用户积分充值
     */
    @PostMapping("/recharge/{userId}")
    public ResponseEntity<Boolean> rechargePoints(
            @PathVariable Long userId,
            @RequestBody Map<String, Object> request) {
        Long points = Long.valueOf(request.get("points").toString());
        String description = (String) request.get("description");
        boolean success = userPointsService.rechargePoints(userId, points, description);
        return ResponseEntity.ok(success);
    }

    /**
     * 用户积分消费
     */
    @PostMapping("/consume/{userId}")
    public ResponseEntity<Boolean> consumePoints(
            @PathVariable Long userId,
            @RequestBody Map<String, Object> request) {
        Long points = Long.valueOf(request.get("points").toString());
        String description = (String) request.get("description");
        boolean success = userPointsService.consumePoints(userId, points, description);
        return ResponseEntity.ok(success);
    }

    /**
     * 批量充值积分
     */
    @PostMapping("/batch/recharge")
    public ResponseEntity<Boolean> batchRechargePoints(@RequestBody Map<String, Object> request) {
        List<Integer> userIdsInt = (List<Integer>) request.get("userIds");
        List<Long> userIds = userIdsInt.stream()
                .map(Long::valueOf)
                .collect(Collectors.toList());
        Long points = Long.valueOf(request.get("points").toString());
        String description = (String) request.get("description");
        boolean success = userPointsService.batchRechargePoints(userIds, points, description);
        return ResponseEntity.ok(success);
    }

    /**
     * 批量消费积分
     */
    @PostMapping("/batch/consume")
    public ResponseEntity<Boolean> batchConsumePoints(@RequestBody Map<String, Object> request) {
        List<Integer> userIdsInt = (List<Integer>) request.get("userIds");
        List<Long> userIds = userIdsInt.stream()
                .map(Long::valueOf)
                .collect(Collectors.toList());
        Long points = Long.valueOf(request.get("points").toString());
        String description = (String) request.get("description");
        boolean success = userPointsService.batchConsumePoints(userIds, points, description);
        return ResponseEntity.ok(success);
    }

    /**
     * 冻结积分
     */
    @PostMapping("/freeze/{userId}")
    public ResponseEntity<Boolean> freezePoints(
            @PathVariable Long userId,
            @RequestBody Map<String, Object> request) {
        Long points = Long.valueOf(request.get("points").toString());
        boolean success = userPointsService.freezePoints(userId, points);
        return ResponseEntity.ok(success);
    }

    /**
     * 解冻积分
     */
    @PostMapping("/unfreeze/{userId}")
    public ResponseEntity<Boolean> unfreezePoints(
            @PathVariable Long userId,
            @RequestBody Map<String, Object> request) {
        Long points = Long.valueOf(request.get("points").toString());
        boolean success = userPointsService.unfreezePoints(userId, points);
        return ResponseEntity.ok(success);
    }
} 